<template>
  <div class="login-container">
    <!-- 左侧装饰区 -->
    <div class="left-section">
      <div class="content">
        <div class="logo-wrapper">
          <n-icon :size="80" color="#fff">
            <BookOutline />
          </n-icon>
        </div>
        <h1 class="title">图书管理系统</h1>
        <p class="subtitle">Library Management System</p>
        <div class="features">
          <div class="feature-item">
            <n-icon :size="24" color="#fff">
              <CheckmarkCircleOutline />
            </n-icon>
            <span>智能借阅管理</span>
          </div>
          <div class="feature-item">
            <n-icon :size="24" color="#fff">
              <CheckmarkCircleOutline />
            </n-icon>
            <span>图书分类检索</span>
          </div>
          <div class="feature-item">
            <n-icon :size="24" color="#fff">
              <CheckmarkCircleOutline />
            </n-icon>
            <span>数据统计分析</span>
          </div>
        </div>
      </div>
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>

    <!-- 右侧表单区 -->
    <div class="right-section">
      <div class="form-wrapper">
        <div class="form-header">
          <h2>{{ activeTab === 'login' ? '欢迎回来' : '创建账号' }}</h2>
          <p>{{ activeTab === 'login' ? '登录您的账号以继续' : '注册一个新账号' }}</p>
        </div>

        <!-- 登录表单 -->
        <div v-show="activeTab === 'login'" class="form-content">
          <n-form ref="loginFormRef" :model="loginForm" :rules="loginRules" size="large">
            <n-form-item path="username">
              <n-input 
                v-model:value="loginForm.username" 
                placeholder="用户名"
                :input-props="{ autocomplete: 'username' }"
              >
                <template #prefix>
                  <n-icon :size="18" color="#999">
                    <PersonOutline />
                  </n-icon>
                </template>
              </n-input>
            </n-form-item>
            <n-form-item path="password">
              <n-input 
                v-model:value="loginForm.password" 
                type="password" 
                show-password-on="click"
                placeholder="密码"
                :input-props="{ autocomplete: 'current-password' }"
                @keyup.enter="handleLogin"
              >
                <template #prefix>
                  <n-icon :size="18" color="#999">
                    <LockClosedOutline />
                  </n-icon>
                </template>
              </n-input>
            </n-form-item>
            <n-button 
              type="primary" 
              block 
              size="large"
              @click="handleLogin" 
              :loading="loading"
              style="margin-top: 8px;"
            >
              登录
            </n-button>
          </n-form>
          
          <div class="form-footer">
            <span>还没有账号？</span>
            <n-button text type="primary" @click="activeTab = 'register'">
              立即注册
            </n-button>
          </div>
        </div>

        <!-- 注册表单 -->
        <div v-show="activeTab === 'register'" class="form-content">
          <n-form ref="registerFormRef" :model="registerForm" :rules="registerRules" size="large">
            <n-form-item path="username">
              <n-input 
                v-model:value="registerForm.username" 
                placeholder="用户名"
                :input-props="{ autocomplete: 'username' }"
              >
                <template #prefix>
                  <n-icon :size="18" color="#999">
                    <PersonOutline />
                  </n-icon>
                </template>
              </n-input>
            </n-form-item>
            <n-form-item path="password">
              <n-input 
                v-model:value="registerForm.password" 
                type="password"
                placeholder="密码"
                :input-props="{ autocomplete: 'new-password' }"
              >
                <template #prefix>
                  <n-icon :size="18" color="#999">
                    <LockClosedOutline />
                  </n-icon>
                </template>
              </n-input>
            </n-form-item>
            <n-form-item path="nickname">
              <n-input 
                v-model:value="registerForm.nickname" 
                placeholder="昵称（可选）"
              >
                <template #prefix>
                  <n-icon :size="18" color="#999">
                    <PersonCircleOutline />
                  </n-icon>
                </template>
              </n-input>
            </n-form-item>
            <n-form-item path="email">
              <n-input 
                v-model:value="registerForm.email" 
                placeholder="邮箱（可选）"
                :input-props="{ autocomplete: 'email' }"
              >
                <template #prefix>
                  <n-icon :size="18" color="#999">
                    <MailOutline />
                  </n-icon>
                </template>
              </n-input>
            </n-form-item>
            <n-button 
              type="primary" 
              block 
              size="large"
              @click="handleRegister" 
              :loading="loading"
              style="margin-top: 8px;"
            >
              注册
            </n-button>
          </n-form>
          
          <div class="form-footer">
            <span>已有账号？</span>
            <n-button text type="primary" @click="activeTab = 'login'">
              立即登录
            </n-button>
          </div>
        </div>

        <!-- 测试账号提示 -->
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { NForm, NFormItem, NInput, NButton, NIcon, NTag, NDivider, useMessage } from 'naive-ui'
import { 
  BookOutline, 
  PersonOutline, 
  LockClosedOutline, 
  PersonCircleOutline, 
  MailOutline,
  CheckmarkCircleOutline 
} from '@vicons/ionicons5'

const router = useRouter()
const userStore = useUserStore()
const message = useMessage()

const activeTab = ref('login')
const loading = ref(false)

const loginForm = ref({
  username: '',
  password: ''
})

const registerForm = ref({
  username: '',
  password: '',
  nickname: '',
  email: ''
})

const loginRules = {
  username: { required: true, message: '请输入用户名', trigger: 'blur' },
  password: { required: true, message: '请输入密码', trigger: 'blur' }
}

const registerRules = {
  username: { required: true, message: '请输入用户名', trigger: 'blur' },
  password: { required: true, message: '请输入密码', trigger: 'blur' }
}

const loginFormRef = ref(null)
const registerFormRef = ref(null)

const handleLogin = async () => {
  try {
    await loginFormRef.value?.validate()
    loading.value = true
    await userStore.login(loginForm.value)
    message.success('登录成功')
    router.push('/')
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

const handleRegister = async () => {
  try {
    await registerFormRef.value?.validate()
    loading.value = true
    await userStore.register(registerForm.value)
    message.success('注册成功，请登录')
    activeTab.value = 'login'
    registerForm.value = { username: '', password: '', nickname: '', email: '' }
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  overflow: hidden;
}

/* 左侧装饰区 */
.left-section {
  flex: 1;
  background: linear-gradient(135deg, #18A058 0%, #0d7a3f 100%);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.left-section .content {
  position: relative;
  z-index: 2;
  text-align: center;
  color: white;
  padding: 60px;
}

.logo-wrapper {
  margin-bottom: 30px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

.title {
  font-size: 48px;
  font-weight: 700;
  margin: 0 0 16px 0;
  letter-spacing: 2px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.subtitle {
  font-size: 18px;
  margin: 0 0 50px 0;
  opacity: 0.95;
  font-weight: 300;
  letter-spacing: 1px;
}

.features {
  max-width: 400px;
  margin: 0 auto;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  font-size: 16px;
  opacity: 0.95;
}

/* 装饰圆圈 */
.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.circle-1 {
  width: 400px;
  height: 400px;
  top: -100px;
  right: -100px;
  animation: pulse 4s ease-in-out infinite;
}

.circle-2 {
  width: 300px;
  height: 300px;
  bottom: -80px;
  left: -80px;
  animation: pulse 5s ease-in-out infinite;
}

.circle-3 {
  width: 200px;
  height: 200px;
  top: 50%;
  left: 10%;
  animation: pulse 6s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 0.1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.15;
  }
}

/* 右侧表单区 */
.right-section {
  flex: 1;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.form-wrapper {
  width: 100%;
  max-width: 450px;
  animation: slideInRight 0.5s ease-out;
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.form-header {
  margin-bottom: 40px;
  text-align: center;
}

.form-header h2 {
  font-size: 32px;
  font-weight: 700;
  color: #333;
  margin: 0 0 12px 0;
}

.form-header p {
  font-size: 15px;
  color: #666;
  margin: 0;
}

.form-content {
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.form-footer {
  margin-top: 24px;
  text-align: center;
  font-size: 14px;
  color: #666;
}

.form-footer span {
  margin-right: 8px;
}

/* 测试账号提示 */
.test-account {
  margin-top: 32px;
  padding: 20px;
  background: #f5f5f5;
  border-radius: 12px;
}

.account-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.account-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: #666;
  padding: 8px 12px;
  background: white;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.account-item:hover {
  background: #18A058;
  color: white;
  transform: translateX(5px);
  box-shadow: 0 4px 12px rgba(24, 160, 88, 0.2);
}

.account-item span {
  font-family: 'Courier New', monospace;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .left-section {
    display: none;
  }
  
  .right-section {
    flex: 1;
  }
}

@media (max-width: 480px) {
  .right-section {
    padding: 20px;
  }
  
  .form-wrapper {
    max-width: 100%;
  }
  
  .form-header h2 {
    font-size: 24px;
  }
}
</style>

